package com.yys.gulimall.order.config;

import com.rabbitmq.client.Channel;
import com.yys.common.constant.WareConstant;
import com.yys.gulimall.order.entity.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import static com.yys.gulimall.order.constant.OrderConstant.*;

/**
 * @author zq
 * @version 1.0
 * @date 2021/4/5 15:20
 */
@Configuration
public class MyMQConfig {
    /**
     * 我们使用如下方式
     * 注意属性发生变化之后不会覆盖,需要删掉重新创建
     * c ---------->   order-event-exchange     -----order.create.key--------->   order-create-queue
     *                       |           ^                                                 |
     *                order.release.key  |                                                 |
     *                       √           |                                                 √
     *        order-release-queue        --------------------------------------------------
     * */
    @Bean
    public Queue orderCreateQueue() {
        /*
            设置属性
            x-dead-letter-exchange: order-event-exchange
            x-dead-letter-routing-key: order.release.binding
            x-message-ttl: 60000

         */
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",ORDER_EVENT_EXCHANGE);
        args.put("x-dead-letter-routing-key",ORDER_RELEASE_ROUTING_KEY);
        args.put("x-message-ttl",ORDER_TTL);

        return new Queue(
            ORDER_CREATE_QUEUE,
                true,
                false,
                false,
                args
        );
    }

    @Bean
    public Queue orderReleaseQueue() {
        return new Queue(
                ORDER_RELEASE_QUEUE,
                true,
                false,
                false
        );
    }

    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(
                ORDER_EVENT_EXCHANGE,
                true,
                false
        );
    }

    @Bean
    public Binding orderCreateBinding() {
        return new Binding(
                ORDER_CREATE_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE,
                ORDER_CREATE_ROUTING_KEY,
                null
        );
    }

    @Bean
    public Binding orderReleaseBinding() {
        return new Binding(
                ORDER_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE,
                ORDER_RELEASE_ROUTING_KEY,
                null
        );
    }

    /**
     * 为了防止由于网络问题导致订单关单在库存释放之后执行
     * 我们在订单关单的时候再主动去发一条释放库存消息
     * @return binding
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding(
                WareConstant.STOCK_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE,
                ORDER_RELEASE_OHTER_ROUTING_KEY,
                null
        );
    }



}
